package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserFamily;
import com.lantosec.hmrp.modules.archives.mapper.UserFamilyMapper;
import com.lantosec.hmrp.modules.archives.service.IUserFamilyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserFamilyVO;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 家庭主要成员及主要社会关系表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-11
 */
@Service
@Primary
@Log4j2
public class UserFamilyServiceImpl extends ServiceImpl<UserFamilyMapper, UserFamily> implements IUserFamilyService {
    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    @Override
    public void saveUserFamily(UserFamilyVO vo) {
        UserFamily userFamily = vo.convertToEntity();
        userFamily.setUserFamilyId(UUIDUtils.get());
        //获取当前用户id和名称
        userFamily.setCreateId(TokenUtil.getUserId());
        userFamily.setCreateUser(TokenUtil.getName());
        userFamily.setCreateTime(DateUtils.nowDate());
        userFamily.setVerifyStatus(1);
        this.save(userFamily);
    }

    @Override
    public void updateUserFamily(UserFamilyVO vo) {
        String userFamilyId = vo.getUserFamilyId();
        //新增一条数据
        //saveUserFamily(vo);
        //修改这条数据的审核状态为2
        if(StringUtils.isNotEmpty(userFamilyId)){
            UserFamily userFamily = vo.convertToEntity();
            userFamily.setVerifyStatus(1);
            this.updateById(userFamily);
        }else {
            saveUserFamily(vo);
        }
    }

    @Override
    public List<UserFamilyVO> getUserFamily(String userId) {
        List<UserFamilyVO> userFamilyVOList = new ArrayList<>();
        List<UserFamily> userFamilyList = this.list(new QueryWrapper<UserFamily>().eq(UserFamily.ColName.user_id.toString(),userId).orderByDesc(UserFamily.ColName.create_time.toString()));
        userFamilyList.forEach(param->{
            UserFamilyVO userFamilyVO = getUserFamilyVo(param);
            userFamilyVOList.add(userFamilyVO);
        });
        return userFamilyVOList;
    }

    /**
     * 组装领导家庭主要成员信息
     * @param userFamily
     */
    private UserFamilyVO getUserFamilyVo(UserFamily userFamily) {
        UserFamilyVO userFamilyVO = userFamily.convertToVO();
        try {
            //查询字典表对应的称谓
            String appellation = userFamily.getAppellation();
            String appellationName = iSysDictionariesService.getById(appellation).getDictionariesShowName();
            userFamilyVO.setAppellationName(appellationName);
            //查询字典表对应的政治面貌
            String politicalStatus = userFamily.getPoliticalStatus();
            String politicalStatusName = iSysDictionariesService.getById(politicalStatus).getDictionariesShowName();
            userFamilyVO.setPoliticalStatusName(politicalStatusName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userFamilyVO;
    }

    @Override
    public List<UserFamilyVO> getUserFamilyVerify(String userId) {
        List<UserFamilyVO> userFamilyVOList = new ArrayList<>();
        List<UserFamily> userFamilyList = this.list(new QueryWrapper<UserFamily>().eq(UserFamily.ColName.user_id.toString(),userId).in(UserFamily.ColName.verify_status.toString(),0,2));
        userFamilyList.forEach(param->{
            UserFamilyVO userFamilyVO = getUserFamilyVo(param);
            userFamilyVOList.add(userFamilyVO);
        });
        return userFamilyVOList;
    }

    @Override
    public void verifyUserFamily(UserFamilyVO vo) {
        UserFamily userFamily = vo.convertToEntity();
        Integer verifyStatus = userFamily.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userFamily.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userFamily.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userFamily.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userFamily.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userFamily.setVerifyId(TokenUtil.getUserId());
        userFamily.setVerifyUser(TokenUtil.getName());
        userFamily.setVerifyTime(DateUtils.nowDate());
        this.updateById(userFamily);
    }

    @Override
    public List<UserFamilyVO> getUserFamilyHistory(String userId) {
        List<UserFamilyVO> userFamilyVOList = new ArrayList<>();
        List<UserFamily> userFamilyList = this.list(new QueryWrapper<UserFamily>().eq(UserFamily.ColName.user_id.toString(),userId).eq(UserFamily.ColName.verify_status.toString(),4));
        userFamilyList.forEach(param->{
            UserFamilyVO userFamilyVO = getUserFamilyVo(param);
            userFamilyVOList.add(userFamilyVO);
        });
        return userFamilyVOList;
    }
}
